<template>
  <el-container>
    <el-header>
      <div>
        <img src="../assets/heima.png" alt="" />
        <span>电商后台管理系统</span>
      </div>
      <el-button type="info" @click="logout">退出</el-button>
    </el-header>
    <el-container>
      <el-aside :width="isCollapse ? '64px' : '200px'">
        <!-- 折叠按钮 -->
        <div class="toggle-button" @click="toggleCollpase">|||</div>
        <!-- 侧边栏菜单区域 -->
        <el-menu
          background-color="#333744"
          text-color="#fff"
          active-text-color="#409EFF"
          :unique-opened="true"
          :collapse="isCollapse"
          :collapse-transition="false"
          router
          :default-active="activePath"
        >
          <!-- 一级菜单 -->
          <el-submenu
            :index="menuitem.id + ''"
            v-for="menuitem in menulist"
            :key="menuitem.id"
          >
            <!-- 一级菜单的模板区域 -->
            <template slot="title">
              <!-- 图标 -->
              <i :class="iconObj[menuitem.id]"></i>
              <!-- 文本 -->
              <span>{{ menuitem.authName }}</span>
            </template>

            <!-- 二级菜单 -->
            <el-menu-item
              :index="'/'+subitem.path"
              v-for="subitem in menuitem.children"
              :key="subitem.id"
              @click="savePathStatus('/'+subitem.path)"
            >
              <template slot="title">
                <!-- 图标 -->
                <i class="el-icon-menu"></i>
                <!-- 文本 -->
                <span>{{ subitem.authName }}</span>
              </template>
            </el-menu-item>
          </el-submenu>
        </el-menu>
      </el-aside>
      <el-main>
        <!-- 路由占位符 -->
        <router-view></router-view>
      </el-main>
    </el-container>
  </el-container>
</template>
<script>
export default {
  data() {
    return {
      //左侧菜单
      menulist: [],
      //一级菜单图标
      iconObj: {
        101: 'iconfont icon-user',
        102: 'iconfont icon-tijikongjian',
        103: 'iconfont icon-shangpin',
        104: 'iconfont icon-danju',
        105: 'iconfont icon-baobiao',
      },
      isCollapse: false,
      //激活菜单的索引
      activePath:''
    }
  },
  created() {
    this.getMenuList()
    this.activePath=window.sessionStorage.getItem('activePath')
  },
  methods: {
    logout() {
      window.sessionStorage.clear()
      this.$router.push('/login')
    },

    getMenuList() {
      this.menulist = [
        {
          id: 101,
          authName: '用户管理',
          path: null,
          pid: 0,
          children: [
            {
              id: 104,
              authName: '用户列表',
              path: 'users',
              pid: 101,
            },
          ],
        },
        {
          id: 102,
          authName: '权限管理',
          path: null,
          pid: 0,
          children: [
            {
              id: 105,
              authName: '角色列表',
              path: 'roles',
              pid: 102,
            },
            {
              id: 106,
              authName: '权限列表',
              path: 'rights',
              pid: 102,
            },
          ],
        },
        {
          id: 103,
          authName: '商品管理',
          path: null,
          pid: 0,
          children: [
            {
              id: 107,
              authName: '商品列表',
              path: 'goods',
              pid: 103,
            },
            {
              id: 108,
              authName: '分类参数',
              path: 'params',
              pid: 103,
            },
            {
              id: 109,
              authName: '商品分类',
              path: 'categorys',
              pid: 103,
            },
          ],
        },
        {
          id: 104,
          authName: '订单管理',
          path: null,
          pid: 0,
          children: [
            {
              id: 110,
              authName: '订单列表',
              path: 'orders',
              pid: 104,
            },
            {
              id: 111,
              authName: '订单分类',
              path: 'ordercategorys',
              pid: 104,
            },
          ],
        },
        {
          id: 105,
          authName: '数据统计',
          path: null,
          pid: 0,
          children: [
            {
              id: 111,
              authName: '数据列表',
              path: null,
              pid: 105,
            },
          ],
        },
      ]
    },

    //侧边栏折叠
    toggleCollpase() {
      this.isCollapse = !this.isCollapse
    },

    savePathStatus(activePath){
      window.sessionStorage.setItem('activePath',activePath)
      this.activePath=activePath

    }
  },
}
</script>

<style lang="less" scoped>
.el-container {
  height: 100%;
}

.el-header {
  background-color: #373d41;
  display: flex;
  justify-content: space-between;
  padding-left: 0px;
  align-items: center;
  color: #fff;
  font-size: 20px;
  > div {
    display: flex;
    align-items: center;
    span {
      margin-left: 15px;
    }
  }
}

.el-aside {
  background-color: #333744;
  .el-menu {
    border-right: none;
  }

  .toggle-button {
    background-color: #4a5064;
    font-size: 10px;
    line-height: 24px;
    color: #fff;
    text-align: center;
    cursor: pointer;
  }
}

.el-main {
  background-color: #eaedf1;
}

.iconfont {
  margin-right: 10px;
}
</style>